package damon.utils;

import org.apache.commons.codec.binary.Hex;

public class DataToHexStrUtil {
    /**
     * 数字转化为16进制字符串，并根据字节长度自动补0调整输出长度
     * @param l
     * @param byteLen
     * @return
     */
    static public String numToHex(long l, int byteLen) throws Exception {
        String hexString = "";
        String numStr = String.valueOf(l);
        if(numStr.toCharArray()[0]=='-'){
            long change = ~(Math.abs(l) - 1l);
            hexString = Long.toHexString(change).substring(2*(8-byteLen));
        }else{
            hexString=Long.toHexString(l);
            if(hexString.length()>byteLen*2){
                throw new Exception("转换后的16进制字符串已超出当前限定字节长度");
            }
            if(byteLen>4 || byteLen<1){
                throw new Exception("字节长度不能小于0或大于4");
            }
            String zeroStr = "";
            for(int i=0;i<byteLen*2-hexString.length();i++){
                zeroStr += "0";
            }
            hexString = zeroStr+hexString;
        }

        return hexString;
    }


    static public String strToHex(String s){
        return Hex.encodeHexString(s.getBytes());
    }

    static public String floatToHex(float f){
        int intBits = Float.floatToIntBits(f);//将float里面的二进制串解释为int整数
        byte[] b = new byte[4];
        b[0] = (byte) ((intBits & 0xff000000) >> 24);
        b[1] = (byte) ((intBits & 0x00ff0000) >> 16);
        b[2] = (byte) ((intBits & 0x0000ff00) >> 8);
        b[3] = (byte)  (intBits & 0x000000ff);
        return Hex.encodeHexString(b);
    }

    public static String doubleToHex(double d){
        long l = Double.doubleToLongBits(d);
        byte[] b = new byte[8];
        b[0] = (byte)  (0xff & (l >> 56));
        b[1] = (byte)  (0xff & (l >> 48));
        b[2] = (byte)  (0xff & (l >> 40));
        b[3] = (byte)  (0xff & (l >> 32));
        b[4] = (byte)  (0xff & (l >> 24));
        b[5] = (byte)  (0xff & (l >> 16));
        b[6] = (byte)  (0xff & (l >> 8));
        b[7] = (byte)  (0xff & l);
        return Hex.encodeHexString(b);
    }

    public static void main(String[] args) throws Exception {
//        System.out.println(Integer.parseUnsignedInt("4294967297"));
//        System.out.println(Long.parseLong("4294967295"));
//        System.out.println(Long.toHexString(Long.parseLong("4294967295")));
        System.out.println(Integer.toHexString(2104967295));
        System.out.println(Long.toHexString(4294967295l));
//        System.out.println(Integer.toHexString());-1
//        System.out.println(Integer.toHexString(-2));
//        System.out.println(intToHex(65535,4));
//        System.out.println(strToHex("888"));
//        System.out.println(floatToHex(1.6f));
//        System.out.println(doubleToHex(2.9));
//        System.out.println(Integer.parseInt("11111111111111111111111111111001", 2));
        System.out.println(Integer.valueOf("-101", 2));
        System.out.println(String.valueOf(-1));
        System.out.println(numToHex(4294967295l,4));
        System.out.println(~(byte)0);
        System.out.println(Long.toHexString(-1l).substring(2*(8-1)));
//        long l = -17865431l;
//        long change = ~(Math.abs(l) - 1l);
//        String hexString = Long.toHexString(change).substring(3);
//        System.out.println(hexString);
        System.out.println(numToHex(-1l,2));
    }
}
